package com.example.agentweblearningproject.fragment;

import android.os.Bundle;
import android.view.View;
import android.webkit.DownloadListener;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.download.library.ResourceRequest;
import com.example.agentweblearningproject.R;
import com.just.agentweb.AbsAgentWebSettings;
import com.just.agentweb.AgentWeb;
import com.just.agentweb.DefaultDownloadImpl;
import com.just.agentweb.IAgentWebSettings;
import com.just.agentweb.WebListenerManager;
import com.just.agentweb.WebViewClient;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.base.XPageFragment;
import com.xuexiang.xpage.utils.TitleBar;
import com.xuexiang.xui.widget.dialog.bottomsheet.BottomSheet;

import butterknife.BindView;

@Page(name = "测试")
public class AgentWebFragment extends XPageFragment {
    public static final String KEY_TITLE = "title";
    private String title;

    @BindView(R.id.linearLayout)
    LinearLayout view;

    private AgentWeb mAgentWeb;
    @Override
    protected void initArgs() {
        super.initArgs();
        Bundle bundle = getArguments();
        if (bundle != null) {
            title = bundle.getString(KEY_TITLE);
        }
    }

    @Override
    protected TitleBar initTitleBar() {
        TitleBar titleBar =  super.initTitleBar();
        titleBar.setTitle(title);
        titleBar.setLeftText("关闭");
        titleBar.setBackgroundResource(R.color.colorPrimary);
        titleBar.addAction(new TitleBar.ImageAction(R.mipmap.more) {
            @Override
            public void performAction(View view) {
                showBottomSheet();
            }
        });
        return titleBar;
    }

    private void showBottomSheet() {
        new BottomSheet.BottomListSheetBuilder(getContext())
                .addItem("刷新页面")
                .addItem("复制链接")
                .addItem("显示错误信息")
                .addItem("在浏览器打开")
                .setOnSheetItemClickListener(new BottomSheet.BottomListSheetBuilder.OnSheetItemClickListener() {
                    @Override
                    public void onClick(BottomSheet dialog, View itemView, int position, String tag) {
                        dialog.dismiss();
                    }
                })
                .build()
                .show();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_agentweb;
    }

    @Override
    protected void initViews() {
        mAgentWeb = AgentWeb.with(this)
                .setAgentWebParent((LinearLayout)view, new LinearLayout.LayoutParams(-1, -1))
                .useDefaultIndicator(-1,3)
                .setAgentWebWebSettings(getSettings())
                .setWebViewClient(new WebViewClient())
                .createAgentWeb()
                .ready()
                .go("http://www.jd.com");

    }

    @Override
    protected void initListeners() {

    }

    public IAgentWebSettings getSettings(){
        return new AbsAgentWebSettings() {
            private AgentWeb agentWeb;
            @Override
            protected void bindAgentWebSupport(AgentWeb agentWeb) {
                this.agentWeb = agentWeb;
            }

            @Override
            public WebListenerManager setDownloader(WebView webView, DownloadListener downloadListener) {
                return super.setDownloader(webView,
                        new DefaultDownloadImpl(getActivity(),webView,
                                this.agentWeb.getPermissionInterceptor()){
                            @Override
                            protected ResourceRequest createResourceRequest(String url) {
                                return super.createResourceRequest(url);
                            }
                        });
            }
        };
    }

    protected com.just.agentweb.WebViewClient mWebViewClient = new WebViewClient(){

    };
}
